package com.xinzhi.service.impl;

import com.xinzhi.dao.TokenMapper;
import com.xinzhi.dao.UserMapper;
import com.xinzhi.model.Result;
import com.xinzhi.model.User;
import com.xinzhi.service.IUserService;
import com.xinzhi.utils.MD5Utils;
import com.xinzhi.utils.RegUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;


@Service
@Transactional
public class UserServiceImpl implements IUserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TokenMapper tokenMapper;

    @Override
    public Result register(User user) {
        // 1 做参数的校验
        //用户名校验
        String username = user.getUsername();
        if (username != null && username != "") {
            boolean check = RegUtils.check(username, "^(?=[a-zA-Z])(?!.*[\\-_]{2})[a-zA-Z0-9\\-_]{8,12}$");
            if (!check) {
                return Result.error("用户名格式不正确");
            }
        } else {
            return Result.error("用户名不能为空");
        }
        //是否注册过
        User user1 = userMapper.findUserByUsername(username);
        if (user1 != null) {
            return Result.error("用户名已存在，请重新输入");
        }
        //密码校验
        String password = user.getPassword();
        if (password != null && password != "") {
            boolean check = RegUtils.check(password, "^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]).{8,}$");
            if (!check) {
                return Result.error("密码格式不正确");
            }
        } else {
            return Result.error("密码不能为空");
        }

        int age = user.getAge();
        if (age > 120 || age < 18) {
            return Result.error("年龄不符合");
        }

        //手机号的校验
        String phone = user.getPhone();
        if (phone != null && phone != "") {
            boolean check = RegUtils.check(phone, "^(1[3-9]\\d{9})$");
            if (!check) {
                return Result.error("手机号格式不正确");
            }
        } else {
            return Result.error("手机号不能为空");
        }
        // 2 然后数据库插入数据
        user.setPassword(MD5Utils.encrypt(password));
        int insert = userMapper.insert(user);
        return insert > 0 ? Result.ok() : Result.error("注册失败");

    }

    @Override
    public Result login(User user) {

        String username = user.getUsername();
        if (username != null && username != "") {
            boolean check = RegUtils.check(username, "^(?=[a-zA-Z])(?!.*[\\-_]{2})[a-zA-Z0-9\\-_]{8,12}$");
            if (!check) {
                return Result.error("用户名格式不正确");
            }
        } else {
            return Result.error("用户名不能为空");
        }

        String password = user.getPassword();
        if (password != null && password != "") {
            boolean check = RegUtils.check(password, "^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]).{8,}$");
            if (!check) {
                return Result.error("密码格式不正确");
            }
        } else {
            return Result.error("密码不能为空");
        }
        password = MD5Utils.encrypt(password);
        User user1 = userMapper.findUserByUsernameAndPassword(username, password);
        if(user1!=null){
            String uuid = UUID.randomUUID().toString();
            String token = uuid.replace("-", "");
            tokenMapper.insert(token, user1.getId());
            return Result.ok("登录成功", token);
        }

        return Result.error("登录失败");
    }

    @Override
    public Result logout(String token) {
        if(token!=null && token!=""){
            tokenMapper.deleteByToken(token);
        }
        return Result.ok();
    }
}
